<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>18.5.8. Queues &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks" href="asyncio.html" />
    <link rel="next" title="18.5.9. Develop with asyncio" href="asyncio-dev.html" />
    <link rel="prev" title="18.5.7. Synchronization primitives" href="asyncio-sync.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="asyncio-dev.html" title="18.5.9. Develop with asyncio"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="asyncio-sync.html" title="18.5.7. Synchronization primitives"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
          <li><a href="asyncio.html" accesskey="U">18.5. <tt class="docutils literal"><span class="pre">asyncio</span></tt> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="queues">
<h1>18.5.8. Queues<a class="headerlink" href="#queues" title="Permalink to this headline">¶</a></h1>
<p>Queues:</p>
<ul class="simple">
<li><a class="reference internal" href="#asyncio.Queue" title="asyncio.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a></li>
<li><a class="reference internal" href="#asyncio.PriorityQueue" title="asyncio.PriorityQueue"><tt class="xref py py-class docutils literal"><span class="pre">PriorityQueue</span></tt></a></li>
<li><a class="reference internal" href="#asyncio.LifoQueue" title="asyncio.LifoQueue"><tt class="xref py py-class docutils literal"><span class="pre">LifoQueue</span></tt></a></li>
<li><a class="reference internal" href="#asyncio.JoinableQueue" title="asyncio.JoinableQueue"><tt class="xref py py-class docutils literal"><span class="pre">JoinableQueue</span></tt></a></li>
</ul>
<p>asyncio queue API was designed to be close to classes of the <a class="reference internal" href="queue.html#module-queue" title="queue: A synchronized queue class."><tt class="xref py py-mod docutils literal"><span class="pre">queue</span></tt></a>
module (<a class="reference internal" href="queue.html#queue.Queue" title="queue.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a>, <a class="reference internal" href="queue.html#queue.PriorityQueue" title="queue.PriorityQueue"><tt class="xref py py-class docutils literal"><span class="pre">PriorityQueue</span></tt></a>,
<a class="reference internal" href="queue.html#queue.LifoQueue" title="queue.LifoQueue"><tt class="xref py py-class docutils literal"><span class="pre">LifoQueue</span></tt></a>), but it has no <em>timeout</em> parameter. The
<a class="reference internal" href="asyncio-task.html#asyncio.wait_for" title="asyncio.wait_for"><tt class="xref py py-func docutils literal"><span class="pre">asyncio.wait_for()</span></tt></a> function can be used to cancel a task after a timeout.</p>
<div class="section" id="queue">
<h2>18.5.8.1. Queue<a class="headerlink" href="#queue" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="asyncio.Queue">
<em class="property">class </em><tt class="descclassname">asyncio.</tt><tt class="descname">Queue</tt><big>(</big><em>maxsize=0</em>, <em>*</em>, <em>loop=None</em><big>)</big><a class="headerlink" href="#asyncio.Queue" title="Permalink to this definition">¶</a></dt>
<dd><p>A queue, useful for coordinating producer and consumer coroutines.</p>
<p>If <em>maxsize</em> is less than or equal to zero, the queue size is infinite. If
it is an integer greater than <tt class="docutils literal"><span class="pre">0</span></tt>, then <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">from</span> <span class="pre">put()</span></tt> will block
when the queue reaches <em>maxsize</em>, until an item is removed by <a class="reference internal" href="#asyncio.Queue.get" title="asyncio.Queue.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a>.</p>
<p>Unlike the standard library <a class="reference internal" href="queue.html#module-queue" title="queue: A synchronized queue class."><tt class="xref py py-mod docutils literal"><span class="pre">queue</span></tt></a>, you can reliably know this Queue&#8217;s
size with <a class="reference internal" href="#asyncio.Queue.qsize" title="asyncio.Queue.qsize"><tt class="xref py py-meth docutils literal"><span class="pre">qsize()</span></tt></a>, since your single-threaded asyncio application won&#8217;t
be interrupted between calling <a class="reference internal" href="#asyncio.Queue.qsize" title="asyncio.Queue.qsize"><tt class="xref py py-meth docutils literal"><span class="pre">qsize()</span></tt></a> and doing an operation on the
Queue.</p>
<p>This class is <a class="reference internal" href="asyncio-dev.html#asyncio-multithreading"><em>not thread safe</em></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4.4: </span>New <a class="reference internal" href="#asyncio.Queue.join" title="asyncio.Queue.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> and <a class="reference internal" href="#asyncio.Queue.task_done" title="asyncio.Queue.task_done"><tt class="xref py py-meth docutils literal"><span class="pre">task_done()</span></tt></a> methods.</p>
</div>
<dl class="method">
<dt id="asyncio.Queue.empty">
<tt class="descname">empty</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.empty" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the queue is empty, <tt class="docutils literal"><span class="pre">False</span></tt> otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.full">
<tt class="descname">full</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.full" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if there are <a class="reference internal" href="#asyncio.Queue.maxsize" title="asyncio.Queue.maxsize"><tt class="xref py py-attr docutils literal"><span class="pre">maxsize</span></tt></a> items in the queue.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If the Queue was initialized with <tt class="docutils literal"><span class="pre">maxsize=0</span></tt> (the default), then
<a class="reference internal" href="#asyncio.Queue.full" title="asyncio.Queue.full"><tt class="xref py py-meth docutils literal"><span class="pre">full()</span></tt></a> is never <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.get">
<em class="property">coroutine </em><tt class="descname">get</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove and return an item from the queue. If queue is empty, wait until
an item is available.</p>
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><em>coroutine</em></a>.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The <a class="reference internal" href="#asyncio.Queue.empty" title="asyncio.Queue.empty"><tt class="xref py py-meth docutils literal"><span class="pre">empty()</span></tt></a> method.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.get_nowait">
<tt class="descname">get_nowait</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.get_nowait" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove and return an item from the queue.</p>
<p>Return an item if one is immediately available, else raise
<a class="reference internal" href="#asyncio.QueueEmpty" title="asyncio.QueueEmpty"><tt class="xref py py-exc docutils literal"><span class="pre">QueueEmpty</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.join">
<em class="property">coroutine </em><tt class="descname">join</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.join" title="Permalink to this definition">¶</a></dt>
<dd><p>Block until all items in the queue have been gotten and processed.</p>
<p>The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer thread calls
<a class="reference internal" href="#asyncio.Queue.task_done" title="asyncio.Queue.task_done"><tt class="xref py py-meth docutils literal"><span class="pre">task_done()</span></tt></a> to indicate that the item was retrieved and all work on
it is complete.  When the count of unfinished tasks drops to zero,
<a class="reference internal" href="#asyncio.Queue.join" title="asyncio.Queue.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> unblocks.</p>
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><em>coroutine</em></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.4.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.put">
<em class="property">coroutine </em><tt class="descname">put</tt><big>(</big><em>item</em><big>)</big><a class="headerlink" href="#asyncio.Queue.put" title="Permalink to this definition">¶</a></dt>
<dd><p>Put an item into the queue. If the queue is full, wait until a free slot
is available before adding item.</p>
<p>This method is a <a class="reference internal" href="asyncio-task.html#coroutine"><em>coroutine</em></a>.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The <a class="reference internal" href="#asyncio.Queue.full" title="asyncio.Queue.full"><tt class="xref py py-meth docutils literal"><span class="pre">full()</span></tt></a> method.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.put_nowait">
<tt class="descname">put_nowait</tt><big>(</big><em>item</em><big>)</big><a class="headerlink" href="#asyncio.Queue.put_nowait" title="Permalink to this definition">¶</a></dt>
<dd><p>Put an item into the queue without blocking.</p>
<p>If no free slot is immediately available, raise <a class="reference internal" href="#asyncio.QueueFull" title="asyncio.QueueFull"><tt class="xref py py-exc docutils literal"><span class="pre">QueueFull</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.qsize">
<tt class="descname">qsize</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.qsize" title="Permalink to this definition">¶</a></dt>
<dd><p>Number of items in the queue.</p>
</dd></dl>

<dl class="method">
<dt id="asyncio.Queue.task_done">
<tt class="descname">task_done</tt><big>(</big><big>)</big><a class="headerlink" href="#asyncio.Queue.task_done" title="Permalink to this definition">¶</a></dt>
<dd><p>Indicate that a formerly enqueued task is complete.</p>
<p>Used by queue consumers. For each <a class="reference internal" href="#asyncio.Queue.get" title="asyncio.Queue.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> used to fetch a task, a
subsequent call to <a class="reference internal" href="#asyncio.Queue.task_done" title="asyncio.Queue.task_done"><tt class="xref py py-meth docutils literal"><span class="pre">task_done()</span></tt></a> tells the queue that the processing
on the task is complete.</p>
<p>If a <a class="reference internal" href="#asyncio.Queue.join" title="asyncio.Queue.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> is currently blocking, it will resume when all items
have been processed (meaning that a <a class="reference internal" href="#asyncio.Queue.task_done" title="asyncio.Queue.task_done"><tt class="xref py py-meth docutils literal"><span class="pre">task_done()</span></tt></a> call was received
for every item that had been <a class="reference internal" href="#asyncio.Queue.put" title="asyncio.Queue.put"><tt class="xref py py-meth docutils literal"><span class="pre">put()</span></tt></a> into the queue).</p>
<p>Raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> if called more times than there were items
placed in the queue.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.4.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="asyncio.Queue.maxsize">
<tt class="descname">maxsize</tt><a class="headerlink" href="#asyncio.Queue.maxsize" title="Permalink to this definition">¶</a></dt>
<dd><p>Number of items allowed in the queue.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="priorityqueue">
<h2>18.5.8.2. PriorityQueue<a class="headerlink" href="#priorityqueue" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="asyncio.PriorityQueue">
<em class="property">class </em><tt class="descclassname">asyncio.</tt><tt class="descname">PriorityQueue</tt><a class="headerlink" href="#asyncio.PriorityQueue" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#asyncio.Queue" title="asyncio.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a>; retrieves entries in priority order (lowest
first).</p>
<p>Entries are typically tuples of the form: (priority number, data).</p>
</dd></dl>

</div>
<div class="section" id="lifoqueue">
<h2>18.5.8.3. LifoQueue<a class="headerlink" href="#lifoqueue" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="asyncio.LifoQueue">
<em class="property">class </em><tt class="descclassname">asyncio.</tt><tt class="descname">LifoQueue</tt><a class="headerlink" href="#asyncio.LifoQueue" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#asyncio.Queue" title="asyncio.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a> that retrieves most recently added entries
first.</p>
</dd></dl>

<div class="section" id="joinablequeue">
<h3>18.5.8.3.1. JoinableQueue<a class="headerlink" href="#joinablequeue" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="asyncio.JoinableQueue">
<em class="property">class </em><tt class="descclassname">asyncio.</tt><tt class="descname">JoinableQueue</tt><a class="headerlink" href="#asyncio.JoinableQueue" title="Permalink to this definition">¶</a></dt>
<dd><p>Deprecated alias for <a class="reference internal" href="#asyncio.Queue" title="asyncio.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a>.</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.4.4.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="exceptions">
<h3>18.5.8.3.2. Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
<dl class="exception">
<dt id="asyncio.QueueEmpty">
<em class="property">exception </em><tt class="descclassname">asyncio.</tt><tt class="descname">QueueEmpty</tt><a class="headerlink" href="#asyncio.QueueEmpty" title="Permalink to this definition">¶</a></dt>
<dd><p>Exception raised when the <a class="reference internal" href="#asyncio.Queue.get_nowait" title="asyncio.Queue.get_nowait"><tt class="xref py py-meth docutils literal"><span class="pre">get_nowait()</span></tt></a> method is called on a
<a class="reference internal" href="#asyncio.Queue" title="asyncio.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a> object which is empty.</p>
</dd></dl>

<dl class="exception">
<dt id="asyncio.QueueFull">
<em class="property">exception </em><tt class="descclassname">asyncio.</tt><tt class="descname">QueueFull</tt><a class="headerlink" href="#asyncio.QueueFull" title="Permalink to this definition">¶</a></dt>
<dd><p>Exception raised when the <a class="reference internal" href="#asyncio.Queue.put_nowait" title="asyncio.Queue.put_nowait"><tt class="xref py py-meth docutils literal"><span class="pre">put_nowait()</span></tt></a> method is called on a
<a class="reference internal" href="#asyncio.Queue" title="asyncio.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a> object which is full.</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">18.5.8. Queues</a><ul>
<li><a class="reference internal" href="#queue">18.5.8.1. Queue</a></li>
<li><a class="reference internal" href="#priorityqueue">18.5.8.2. PriorityQueue</a></li>
<li><a class="reference internal" href="#lifoqueue">18.5.8.3. LifoQueue</a><ul>
<li><a class="reference internal" href="#joinablequeue">18.5.8.3.1. JoinableQueue</a></li>
<li><a class="reference internal" href="#exceptions">18.5.8.3.2. Exceptions</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="asyncio-sync.html"
                        title="previous chapter">18.5.7. Synchronization primitives</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="asyncio-dev.html"
                        title="next chapter">18.5.9. Develop with asyncio</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/asyncio-queue.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="asyncio-dev.html" title="18.5.9. Develop with asyncio"
             >next</a> |</li>
        <li class="right" >
          <a href="asyncio-sync.html" title="18.5.7. Synchronization primitives"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="ipc.html" >18. Interprocess Communication and Networking</a> &raquo;</li>
          <li><a href="asyncio.html" >18.5. <tt class="docutils literal"><span class="pre">asyncio</span></tt> &#8211; Asynchronous I/O, event loop, coroutines and tasks</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 02, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>